package zero.tech.games.gate.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import zero.tech.core.data.Resp;
import zero.tech.core.data.SimpleUser;
import zero.tech.games.api.RemoteExtension;
import zero.tech.games.api.RemoteUser;
import zero.tech.games.common.ClubRoleId;
import zero.tech.games.common.Code;
import zero.tech.games.common.FrozenUtil;
import zero.tech.games.entity.games.Club;
import zero.tech.games.entity.games.ClubGameRule;
import zero.tech.games.entity.games.ClubMember;
import zero.tech.games.entity.games.ClubMemberPermission;
import zero.tech.games.entity.games.ClubMemberProxy;
import zero.tech.games.entity.games.ClubMemberRole;
import zero.tech.games.entity.games.UserOnline;
import zero.tech.games.entity.user.TUser;
import zero.tech.games.gate.data.entity.ReqPermission;
import zero.tech.games.gate.data.entity.ZsInfo;
import zero.tech.games.gate.data.res.ResDirectMembers;
import zero.tech.games.gate.data.res.ResHehuorenTotalTea;
import zero.tech.games.gate.data.res.ResLookClubMember;
import zero.tech.games.gate.data.res.ResLookUser;
import zero.tech.games.gate.data.res.ResSelectGameRules;
import zero.tech.games.gate.data.res.ResSelectHehuorenPermission;
import zero.tech.games.gate.hot.ClubUtil;
import zero.tech.games.gate.mapper.UserOnlineMapper;
import zero.tech.games.gate.service.api.IClubMemberService;
import zero.tech.games.gate.service.api.ITradeService;
import zero.tech.games.gate.util.PackageUtil;

@Service
public class ClubMemberServiceImpl implements IClubMemberService{
	@Autowired
	private RemoteUser remoteUser;
	@Autowired
	private ClubUtil clubUtil;
	@Autowired
	private ITradeService tradeService;
	@Autowired
	private RemoteExtension remoteExtension;
	@Autowired
	private FrozenUtil frozenUtil;
	@Autowired
	private UserOnlineMapper userOnlineMapper;
	@Override
	@Transactional
	public Resp addMember(SimpleUser simpleUser, int uid, int club_id) {
		//检查是否属于茶馆合伙人
		int father_uid=simpleUser.getUid();
		if(father_uid==uid) {
			return Resp.build(Code.CANT_INVITE_SELF, "不能邀请自己");
		}
		TUser user = remoteUser.getUser(uid);
		if(user==null) {
			return Resp.build(Code.USER_NOT_EXIST, "用户不存在");
		}
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMember clubMember = clubUtil.getClubMember(uid, club_id);
		if(clubMember!=null) {
			return Resp.build(Code.CLUBMEMBER_IS_EXSIST, "玩家已经在俱乐部了");
		}
		ClubMemberPermission clubMemberPermission=clubUtil.getClubMemberPermission(club_id,father_uid);
		if(clubMemberPermission==null||clubMemberPermission.getAdd_person()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		clubUtil.insertClubMember(father_uid,uid,club_id,simpleUser,user,club);
		return Resp.build(Code.OK, "添加成员成功");
	}

	@Override
	public Resp directMembers(SimpleUser simpleUser, int club_id, int page, int pageLength,int type) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		List<Integer> frozen_uids=frozenUtil.getFrozenList(club_id);
		if(type==0) {
			//直属玩家
			int uid=simpleUser.getUid();
			ZsInfo zsInfo=clubUtil.getZsUidsByPage(club_id,uid,page,pageLength);
			int total_size=zsInfo.getTotal_size();
			List<Integer> zsUids=zsInfo.getZsuids();
			List<TUser> list=new ArrayList<TUser>();
			if(!zsUids.isEmpty()) {
				list.addAll(remoteUser.getUserBatch(zsUids));
			}
			Map<Integer,UserOnline> onlinemap=new HashMap<>();
			if(!zsUids.isEmpty()) {
				List<UserOnline> userOnlines=userOnlineMapper.selectByUids(zsUids);
				for(UserOnline userOnline:userOnlines) {
					onlinemap.put(userOnline.getUid(), userOnline);
				}
			}
			Map<Integer,TUser> usermap= new HashMap<>();
			Map<Integer,ClubMember> clubmembermap= new HashMap<>();
			Map<Integer,ClubMemberRole> clubMemberRoleMap= new HashMap<>();
			Map<Integer,ClubMemberProxy> clubMemberProxyMap= new HashMap<>();
			Map<Integer,Integer> zs_count_map=remoteExtension.zsCountMap(club_id,JSONObject.toJSONString(zsUids));
			Map<Integer,Integer> fzs_count_map=remoteExtension.fzsCountMap(club_id,JSONObject.toJSONString(zsUids));
			for(TUser user:list) {
				int id=user.getUid();
				ClubMember clubMember = clubUtil.getClubMember(id, club_id);
				ClubMemberRole clubMemberRole=clubUtil.getClubMemberRole(club_id, id);
				ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, id);
				usermap.put(id, user);
				clubmembermap.put(id,clubMember);
				clubMemberRoleMap.put(id, clubMemberRole);
				clubMemberProxyMap.put(id, clubMemberProxy);
			}
			long total_tea=clubUtil.getZsTotalTea(club_id,uid);
			ResDirectMembers resDirectMembers=PackageUtil.packageResDirectMembers(total_size,page,pageLength,zsUids,usermap,clubmembermap,clubMemberRoleMap,total_tea,zs_count_map,fzs_count_map,clubMemberProxyMap,frozen_uids,onlinemap);
			return Resp.build(Code.OK, "获取直属成员成功",resDirectMembers);
		}else {
			//直属合伙人
			int uid=simpleUser.getUid();
			ZsInfo zsInfo=clubUtil.getZsHehuorenByPage(club_id,uid,page,pageLength);
			int total_size=zsInfo.getTotal_size();
			List<Integer> zsUids=zsInfo.getZsuids();
			List<TUser> list=new ArrayList<TUser>();
			if(!zsUids.isEmpty()) {
				list.addAll(remoteUser.getUserBatch(zsUids));
			}
			Map<Integer,UserOnline> onlinemap=new HashMap<>();
			if(!zsUids.isEmpty()) {
				List<UserOnline> userOnlines=userOnlineMapper.selectByUids(zsUids);
				for(UserOnline userOnline:userOnlines) {
					onlinemap.put(userOnline.getUid(), userOnline);
				}
			}
			Map<Integer,TUser> usermap= new HashMap<>();
			Map<Integer,ClubMember> clubmembermap= new HashMap<>();
			Map<Integer,ClubMemberRole> clubMemberRoleMap= new HashMap<>();
			Map<Integer,ClubMemberProxy> clubMemberProxyMap= new HashMap<>();
			Map<Integer,Integer> zs_count_map=remoteExtension.zsCountMap(club_id,JSONObject.toJSONString(zsUids));
			Map<Integer,Integer> fzs_count_map=remoteExtension.fzsCountMap(club_id,JSONObject.toJSONString(zsUids));
			for(TUser user:list) {
				int id=user.getUid();
				ClubMember clubMember = clubUtil.getClubMember(id, club_id);
				ClubMemberRole clubMemberRole=clubUtil.getClubMemberRole(club_id, id);
				ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, id);
				usermap.put(id, user);
				clubmembermap.put(id,clubMember);
				clubMemberRoleMap.put(id, clubMemberRole);
				clubMemberProxyMap.put(id, clubMemberProxy);
			}
			ResDirectMembers resDirectMembers=PackageUtil.packageResDirectMembers(total_size,page,pageLength,zsUids,usermap,clubmembermap,clubMemberRoleMap,0,zs_count_map,fzs_count_map,clubMemberProxyMap,frozen_uids,onlinemap);
			return Resp.build(Code.OK, "获取合伙人成员成功",resDirectMembers);
		}
	}

	@Override
	public Resp lookUser(SimpleUser simpleUser, int uid) {
		TUser user = remoteUser.getUser(uid);
		if(user==null) {
			return Resp.build(Code.USER_NOT_EXIST, "用户不存在");
		}
		UserOnline userOnline=userOnlineMapper.getUserOnline(uid);
		ResLookUser resLookUser = PackageUtil.packageResLookUser(user,userOnline);
		return Resp.build(Code.OK, "查询用户成功",resLookUser);
	}

	@Override
	@Transactional
	public Resp addMoney(SimpleUser simpleUser, int club_id, int uid, long gold, int type) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission==null
				||(clubMemberPermission.getAdd_money()==0&&type==0)
				||(type==1&&clubMemberPermission.getReduce_money()==0)) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		return tradeService.trade(club_id,simpleUser.getUid(),uid,gold,type);
	}

	@Override
	public Resp hehuorenLineTotalTea(SimpleUser simpleUser, int club_id, int uid) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMember clubMember = clubUtil.getClubMember(simpleUser.getUid(), club_id);
		if(clubMember==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		ClubMemberProxy clubMemberProxy = clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "玩家不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		ClubMemberRole clubMemberRole = clubUtil.getClubMemberRole(club_id, uid);
		if(clubMemberRole.getRole()==ClubRoleId.NOMAL) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_HEHUOREN, "玩家不是合伙人");
		}
		List<Integer> allChilds=remoteExtension.allChilds(club_id,uid);
		if(!allChilds.contains(uid)) {
			allChilds.add(uid);
		}
		//开始查询所有子集的茶叶总和
		long total_tea=clubUtil.getHehuorenTotalTea(club_id,simpleUser.getUid(),uid,allChilds);
		ResHehuorenTotalTea resHehuorenTotalTea= new ResHehuorenTotalTea();
		resHehuorenTotalTea.setTotal_tea(total_tea);
		return Resp.build(Code.OK, "查询合伙人线总茶叶成功",resHehuorenTotalTea);
	}

	@Override
	public Resp lookMember(SimpleUser simpleUser, int club_id, int uid) {
		TUser user = remoteUser.getUser(uid);
		if(user==null) {
			return Resp.build(Code.USER_NOT_EXIST, "用户不存在");
		}
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMember myMember = clubUtil.getClubMember(simpleUser.getUid(), club_id);
		if(myMember==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		ClubMemberProxy clubMemberProxy = clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		ClubMember clubMember = clubUtil.getClubMember(uid, club_id);
		UserOnline userOnline = userOnlineMapper.getUserOnline(uid);
		ClubMemberRole clubMemberRole=clubUtil.getClubMemberRole(club_id, uid);
		List<Integer> zsUids=new ArrayList<>();
		zsUids.add(uid);
		Map<Integer,Integer> zs_count_map=remoteExtension.zsCountMap(club_id,JSONObject.toJSONString(zsUids));
		Map<Integer,Integer> fzs_count_map=remoteExtension.fzsCountMap(club_id,JSONObject.toJSONString(zsUids));
		List<Integer> frozen_uids=frozenUtil.getFrozenList(club_id);
		ResLookClubMember resLookClubMember = PackageUtil.packageResLookClubMember(user,clubMember,clubMemberRole,clubMemberProxy,zs_count_map,fzs_count_map,frozen_uids,userOnline);
		return Resp.build(Code.OK, "查询用户成功",resLookClubMember);
	}
	
	@Override
	public Resp lookSubMember(SimpleUser simpleUser, int club_id, int uid) {
		TUser user = remoteUser.getUser(uid);
		if(user==null) {
			return Resp.build(Code.USER_NOT_EXIST, "用户不存在");
		}
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission==null||clubMemberPermission.getLook_allsubs()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMember clubMember = clubUtil.getClubMember(uid, club_id);
		if(clubMember==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		List<Integer> allSubs=remoteExtension.allChilds(club_id, simpleUser.getUid());
		if(!allSubs.contains(uid)) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "用户不是你的");
		}
		UserOnline userOnline=userOnlineMapper.getUserOnline(uid);
		ClubMemberProxy clubMemberProxy = clubUtil.getClubMemberProxy(club_id, uid);
		ClubMemberRole clubMemberRole=clubUtil.getClubMemberRole(club_id, uid);
		List<Integer> uids=new ArrayList<>();
		uids.add(uid);
		Map<Integer,Integer> zs_count_map=remoteExtension.zsCountMap(club_id,JSONObject.toJSONString(uids));
		Map<Integer,Integer> fzs_count_map=remoteExtension.fzsCountMap(club_id,JSONObject.toJSONString(uids));
		List<Integer> frozen_uids=frozenUtil.getFrozenList(club_id);
		ResLookClubMember resLookClubMember = PackageUtil.packageResLookClubMember(user,clubMember,clubMemberRole,clubMemberProxy,zs_count_map,fzs_count_map,frozen_uids,userOnline);
		return Resp.build(Code.OK, "查询用户成功",resLookClubMember);
	}

	@Override
	@Transactional
	public Resp setHehuoren(SimpleUser simpleUser, int club_id, int uid) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission==null||clubMemberPermission.getSet_hehuoren()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		ClubMemberRole clubMemberRole = clubUtil.getClubMemberRole(club_id, uid);
		if(clubMemberRole.getRole()!=ClubRoleId.NOMAL) {
			return Resp.build(Code.CLUBMEMBER_IS_HEHUOREN, "玩家已经是合伙人了");
		}
		//开始设置合伙人
		clubUtil.setHehuoren(club_id,simpleUser.getUid(),uid);
		return Resp.build(Code.OK, "设置合伙人成功");
	}

	@Override
	@Transactional
	public Resp delHehuoren(SimpleUser simpleUser, int club_id, int uid) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission==null||clubMemberPermission.getDel_hehuoren()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		ClubMemberRole clubMemberRole = clubUtil.getClubMemberRole(club_id, uid);
		if(clubMemberRole.getRole()!=ClubRoleId.HEHUOREN) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_HEHUOREN, "玩家不是合伙人");
		}
		//该合伙人下面有玩家时不能取消
		List<Integer> allChilds=remoteExtension.allChilds(club_id,uid);
		if(!allChilds.isEmpty()) {
			return Resp.build(Code.CLUBMEMBER_HAVE_CHILDS, "合伙人有下级玩家不能取消");
		}
		//取消合伙人
		clubUtil.delHehuoren(club_id,simpleUser.getUid(),uid);
		return Resp.build(Code.OK, "取消合伙人成功");
	}

	@Override
	public Resp KickRoom(SimpleUser simpleUser, int club_id, int uid) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission==null||clubMemberPermission.getKick_room()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		//代码在这里
		return null;
	}

	@Override
	public Resp frozenPerson(SimpleUser simpleUser, int club_id, int uid) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission==null||clubMemberPermission.getFrozen_person()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		clubUtil.frozenPerson(club_id,simpleUser.getUid(),uid);
		return Resp.build(Code.OK, "冻结用户成功");
	}

	@Override
	public Resp frozenLine(SimpleUser simpleUser, int club_id, int uid) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission==null||clubMemberPermission.getFrozen_person()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		ClubMemberRole clubMemberRole = clubUtil.getClubMemberRole(club_id, uid);
		if(clubMemberRole.getRole()!=ClubRoleId.HEHUOREN) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_HEHUOREN, "玩家不是合伙人");
		}
		List<Integer> allChilds=remoteExtension.allChilds(club_id,uid);
		if(!allChilds.contains(uid)) {
			allChilds.add(uid);
		}
		clubUtil.frozenLine(club_id,simpleUser.getUid(),allChilds);
		return Resp.build(Code.OK, "冻结用户整线成功");
	}
	
	
	

	@Override
	@Transactional
	public Resp setHehuorenPower(SimpleUser simpleUser, ReqPermission reqPermission) {
		int club_id=reqPermission.getClub_id();
		int uid=reqPermission.getUid();
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission==null||clubMemberPermission.getSet_hehuoren()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		ClubMemberRole clubMemberRole = clubUtil.getClubMemberRole(club_id, uid);
		if(clubMemberRole.getRole()!=ClubRoleId.HEHUOREN) {
			return Resp.build(Code.CLUBMEMBER_IS_HEHUOREN, "玩家不是合伙人");
		}
		//开始设置合伙人权限
		boolean ok=clubUtil.setHehuorenPower(club_id,simpleUser.getUid(),uid,reqPermission);
		if(!ok) {
			return Resp.build(Code.OK, "设置合伙人失败");
		}
		return Resp.build(Code.OK, "设置合伙人成功");
	}

	@Override
	@Transactional
	public Resp setHehuorenFanyong(SimpleUser simpleUser, int club_id, int uid, float fanyong) {
		if(fanyong>1||fanyong<=0) {
			return Resp.build(Code.PARAM_ERROR, "参数错误");
		}
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission.getSet_hehuoren()==null||clubMemberPermission.getSet_hehuoren()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		ClubMemberRole clubMemberRole = clubUtil.getClubMemberRole(club_id, uid);
		if(clubMemberRole.getRole()!=ClubRoleId.HEHUOREN) {
			return Resp.build(Code.CLUBMEMBER_IS_HEHUOREN, "玩家不是合伙人");
		}
		ClubMemberProxy myProxy=clubUtil.getClubMemberProxy(club_id, simpleUser.getUid());
		if(myProxy.getFanyong()<fanyong) {
			return Resp.build(Code.CLUBMEMBER_FANYONG_MUST_LESS_THAN_YOU, "合伙人返佣比必须小于你的返佣比");
		}
		//设置返佣必须比被设置人的直属下级最大值高
		Float tmp=clubUtil.getSubMaxFanyong(club_id,uid);
		if(tmp!=null&&fanyong<=tmp) {
			return Resp.build(Code.PARAM_ERROR, "玩家合伙人返佣必须大于其直属下级");
		}
		//开始设置返佣比
		clubUtil.setHehuorenFanyong(club_id,simpleUser.getUid(),uid,fanyong);
		return Resp.build(Code.OK, "设置合伙人返佣比成功");
	}

	@Override
	public Resp selectHehuorenPower(SimpleUser simpleUser, int club_id, int uid) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMember clubMember = clubUtil.getClubMember(simpleUser.getUid(), club_id);
		if(clubMember==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		ClubMemberRole clubMemberRole = clubUtil.getClubMemberRole(club_id, uid);
		if(clubMemberRole.getRole()!=ClubRoleId.HEHUOREN) {
			return Resp.build(Code.CLUBMEMBER_IS_HEHUOREN, "玩家不是合伙人");
		}
		ClubMemberPermission otherMemberPermission=clubUtil.getClubMemberPermission(club_id, uid);
		ResSelectHehuorenPermission resSelectHehuorenPermission = PackageUtil.packageResSelectHehuorenPermission(otherMemberPermission);
		return Resp.build(Code.OK, "获取玩家权限成功",resSelectHehuorenPermission);
	}

	@Override
	@Transactional
	public Resp frozenPersonGold(SimpleUser simpleUser, int club_id, int uid,long gold) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission.getFrozen_person_gold()==null||clubMemberPermission.getFrozen_person_gold()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		return tradeService.frozenPersonGold(club_id,simpleUser.getUid(),uid,gold);
	}

	@Override
	@Transactional
	public Resp createGameRule(SimpleUser simpleUser, int club_id, int game_id, String pipei_key, String class_name,
			String config_str,int fzb_playernum) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission.getCreate_game_rule()==null||clubMemberPermission.getCreate_game_rule()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		clubUtil.createGameRule(simpleUser.getUid(),club_id,game_id,pipei_key,class_name,config_str,fzb_playernum);
		return Resp.build(Code.OK, "创建玩法成功");
	}

	@Override
	public Resp selectGameRules(SimpleUser simpleUser, int club_id,String class_name, int page, int pageLength) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMember clubMember = clubUtil.getClubMember(simpleUser.getUid(), club_id);
		if(clubMember==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		ResSelectGameRules resSelectGameRules=clubUtil.selectGameRules(simpleUser.getUid(),club_id,class_name,page,pageLength);
		return Resp.build(Code.OK,"分页获取玩法成功", resSelectGameRules);
	}

	@Override
	public Resp switchGameRules(SimpleUser simpleUser, int club_id, int rule_id,int open) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission.getSwitch_game_rule()==null||clubMemberPermission.getSwitch_game_rule()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubGameRule clubGameRule = clubUtil.loadHotClubGameRule(rule_id);
		if(clubGameRule.getClub_id()!=club_id) {
			return Resp.build(Code.PARAM_ERROR, "俱乐部没有该玩法");
		}
		clubUtil.switchGameRule(simpleUser.getUid(),club_id,rule_id,open);
		return Resp.build(Code.OK,"开关玩法成功");
	}

	@Override
	@Transactional
	public Resp operateBaoxiangui(SimpleUser simpleUser, int club_id,long gold, int type) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMember clubMember = clubUtil.getClubMember(simpleUser.getUid(), club_id);
		if(clubMember==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		return tradeService.operateBaoxiangui(club_id,simpleUser.getUid(),gold,type);
	}

	@Override
	public Resp editNotice(SimpleUser simpleUser, int club_id, String notice) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMember clubMember = clubUtil.getClubMember(simpleUser.getUid(), club_id);
		if(clubMember==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		ClubMemberRole clubMemberRole=clubUtil.getClubMemberRole(club_id, simpleUser.getUid());
		if(clubMemberRole.getRole()!=ClubRoleId.GUAN_ZHU) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		clubUtil.editNotice(club_id,simpleUser.getUid(),notice);
		return Resp.build(Code.OK, "编辑公共成功");
	}

	@Override
	public Resp cancleFrozenPerson(SimpleUser simpleUser, int club_id, int uid) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission==null||clubMemberPermission.getFrozen_person()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		clubUtil.cancleFrozenPerson(club_id,simpleUser.getUid(),uid);
		return Resp.build(Code.OK, "解冻用户成功");
	}

	@Override
	@Transactional
	public Resp kickClub(SimpleUser simpleUser, int club_id, int uid) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission==null||clubMemberPermission.getKick_club()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		List<Integer> allChilds=remoteExtension.allChilds(club_id,simpleUser.getUid());
		if(!allChilds.contains(uid)) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "用户不属于你");
		}
		clubUtil.kickClub(club_id,simpleUser.getUid(),uid);
		return Resp.build(Code.OK, "T出茶馆成功");
	}

	@Override
	@Transactional
	public Resp cancleFrozenPersonGold(SimpleUser simpleUser, int club_id, int uid, long gold) {
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		ClubMemberPermission clubMemberPermission = clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		if(clubMemberPermission==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "你不在俱乐部");
		}
		if(clubMemberPermission.getFrozen_person_gold()==null||clubMemberPermission.getFrozen_person_gold()==0) {
			return Resp.build(Code.POWER_NOT_ENOUGH, "权限不够");
		}
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, uid);
		if(clubMemberProxy==null) {
			return Resp.build(Code.CLUBMEMBER_IS_NOT_EXSIST, "用户不在俱乐部");
		}
		if(clubMemberProxy.getFather_uid()==null||(clubMemberProxy.getFather_uid()!=null&&clubMemberProxy.getFather_uid()!=simpleUser.getUid())) {
			return Resp.build(Code.CLUBMEMBER_NOT_YOURS, "玩家不是你的直属下级");
		}
		return tradeService.cancleFrozenPersonGold(club_id,simpleUser.getUid(),uid,gold);
	}

}
